import { create } from 'zustand';
import { devtools } from 'zustand/middleware';

interface UserInfo {
  id: string;
  user_name: string;
  phone: string;
  create_time: string;
}

interface UserInfoStore {
  userInfo: UserInfo;
  token: string;
}

interface UserInfoStoreActions {
  setUserInfo: (userInfo: UserInfo) => void;
  setToken: (token: string) => void;
}

const useUserInfoStore = create(
  devtools<UserInfoStore & UserInfoStoreActions>((set) => ({
    userInfo: JSON.parse(localStorage.getItem('userInfo') || '{}') as UserInfo,
    token: (localStorage.getItem('token') as string) || '',
    setUserInfo: (userInfo: UserInfo) => set({ userInfo }),
    setToken: (token: string) => set({ token }),
  }))
);

export default useUserInfoStore;
